package com.example.hs.Controller;

import com.example.hs.Pojo.Address;
import com.example.hs.Pojo.User;
import com.example.hs.Service.AddressService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @GetMapping("/all")
    public List<Address> getAddressesByUserId(HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<Address> addresses = addressService.getAddressesByUserId(user.getUserId());
        return addresses;
    }
    @PostMapping("/add")
    public Address addAddress(@RequestBody Address address, HttpSession session) {
        Address newAddress = addressService.addAddress(address,session);
        return newAddress;
    }
    @PutMapping("/update/{addressId}")
    public Address updateAddress(@PathVariable int addressId, @RequestBody Address updateAddress) {
        Address updatedAddress = addressService.updateAddress(addressId, updateAddress);
        return updatedAddress;
    }

}
